/*!
 * DOMEffects 0.2.6, Copyright (c) 2008 Pelle Wessman, <http://code.google.com/p/domeffects>, MIT Style License.
 */
/*extern DOMAssistant */
/**
 * @fileOverview Highlights an element with a "yellow-fade"-technique, requires DOMEffects
 * @name DOMEffects Highlight
 */

/** @scope DOMAssistant */
DOMAssistant.attach({
	/**
	 * Highlight the background color
	 * @param  {string|object} [options] The options or a color
	 * @config {string}   [color]        The color to flash, defaults to the "yellow-fade" color #ffff99. Should be in an acceptable CSS-format.
	 * @config {string}   [original]     The original background color in one of these three formats: #fff, #ffddff or rgb(255, 255, 255). Defaults to the background-color set in the CSS or if that is transparent to #fff
	 * @config {integer}  [duration]     Amount of time in milliseconds that the element will be animated, default 1.5 second. If 0 the animation will happen immediatly.
	 * @config {boolean}  [nostop]       Should the existing animations stop? Defaults to true.
	 * @config {function} [easing]       A function describing an easing equation, default linear
	 * @config {function} [callback]     A function to be called once the element has been hidden
	 * @config {boolean}  [front]        Should the front-color be animated instead?
	 * @return {object}                  Returns the element to enable chaining
	 * @example $('div').highlight(); //Flashes the element to the "yellow-fade" color and back to it's original color
	 * @example //Easing function
	 * function easeExpoOut(timediff, base, change, duration) {
	 * 	return (timediff==duration) ?
	 * 	       base+change :
	 * 	       change * (-Math.pow(2, -10 * timediff/duration) + 1) + base;
	 * }
	 * //Show-call with many options set
	 * $('div').highlight({color: '#FF0000', duration: 400, transition: easeExpoOut, callback: function() {
	 * 	console.log('Element highlightet!');
	 * }});
	 */
	highlight: function(options){
		var elem = $$(this),
			optionsType = typeof options,
			isObj = optionsType === 'object',
			constColor = 'color',
			constTargetColor = (isObj && options.front) ? constColor : 'background-color',
			attr = {},
			value = optionsType === 'string' ? options : ((isObj && options[constColor]) ? options[constColor] : '#ff9');

		attr[constTargetColor] = {
			value: (isObj && options.original) ? options.original : elem.getStyle(constTargetColor),
			unit: constColor
		};

		if (attr[constTargetColor].value === 'transparent' || attr[constTargetColor].value === 'rgba(0, 0, 0, 0)') {
			attr[constTargetColor].value = '#fff';
		}

		if (isObj && !options.nostop) {
			elem.stop();
		}

		return elem.setStyle(constTargetColor, value).animate(attr, isObj ? options : {} );
	}
});